package com.share.lottery.activity.caiborecommond;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.share.lottery.R;
import com.share.lottery.adapter.CommendStatisticsAdapter;
import com.share.lottery.base.BaseFragment;
import com.share.lottery.base.Constants;
import com.share.lottery.model.Statics;
import com.share.lottery.model.StaticsBean;
import com.share.lottery.utils.JsonUtil;
import com.share.lottery.utils.StringUtil;

/**
 * @author py
 *方案统计
 *
 */
public class StatisticsFragment extends BaseFragment {
	private PullToRefreshListView pullToRefreshListView = null;
	private View view;
	private CommendStatisticsAdapter adapter;
	private TextView tvZs;//方案总数
	private TextView tvCg;//成功数
	private TextView tvBl;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.statics_fragment,container, false);
		init();
		return view;
	}

	public void findView() {
		
		tvZs = (TextView) view.findViewById(R.id.tv_fazs);
		tvBl = (TextView) view.findViewById(R.id.tv_cgl);
		tvCg = (TextView) view.findViewById(R.id.tv_cgl);
		pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.audio_list);
		pullToRefreshListView.setMode(Mode.BOTH);
		pullToRefreshListView.setOnRefreshListener(new MyOnRefreshListener2(
				pullToRefreshListView));


	}


	public void setData() {
		if (adapter == null) {
			adapter = new CommendStatisticsAdapter(getActivity());
		}
		pullToRefreshListView.setAdapter(adapter);

	}


	public void getData() {
		showProgreessDialog(getActivity());
	//	CaiboRmdController.getInstance().caibaoRmdStatistics(handler);

	}


	public void setOnClicker() {
		pullToRefreshListView.setOnItemClickListener(new OnItemClickListener() {


			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {


			}
		});

	}

	private void doMessage(Message msg) {
		switch (msg.arg1) {
		case Constants.HTTP_OK://请求成功
			Statics statics = (Statics) msg.obj;
			if (statics != null) {
				tvZs.setText("方案总数:"+statics.getTotal());
				tvCg.setText("方案成功:"+statics.getSuccess());
				tvBl.setText("成功率:"+statics.getRatio());
				String deal = statics.getDetail();
				if (!StringUtil.isNullOrEmpty(deal)) {

					List<StaticsBean> AdvertBeanList = (List<StaticsBean>) JsonUtil
							.parseArray(deal,StaticsBean.class);
					adapter.addDatas(AdvertBeanList);

				}

			}
			break;
		case Constants.HTTP_ERROR://请求错误
			String serror = (String) msg.obj;
			showShortToast(serror);
			break;
		case Constants.HTTP_ERROR_PASER://数据解析错误
			showShortToast("数据解析错误");
			break;
		case Constants.HTTP_ERROR_NET://网络错误

			showShortToast("网络错误，请检查网络");
			break;

		default:
			break;
		}

	}

	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			dismissProgressDialog();
			pullToRefreshListView.onRefreshComplete();
			switch (msg.what) {
			case Constants.CAIBO_RMD_STATISTICS:
				doMessage(msg);
				break;

			default:
				break;
			}

		}


	};
	/**
	 * 
	 * @ClassName:MyOnRefreshListener2
	 * @Description:TODO 下拉刷新类
	 * @author py
	 * 
	 */
	class MyOnRefreshListener2 implements OnRefreshListener2<ListView> {

		private PullToRefreshListView mPtflv;

		public MyOnRefreshListener2(PullToRefreshListView ptflv) {
			this.mPtflv = ptflv;

		}

		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			// 下拉刷新
			String mLabel = DateUtils.formatDateTime(
					getActivity().getApplicationContext(),
					System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
					| DateUtils.FORMAT_SHOW_DATE
					| DateUtils.FORMAT_ABBREV_ALL);

			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(mLabel);


		}



		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			// 上拉加载

		}

	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}



}
